package com.reqresp; /**
 * @author wzm
 * @create 2023-09-19 14:28
 * project_name:javaweb2308
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@WebServlet("/reqServlet02")
public class ReqServlet02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);

       /* String username = request.getParameter("username");//假如get乱码
        //乱码前  &&&987644aaa
        //get请求获取参数 tomcat进行url编码 默认字符集ISO-8859-1
        //对乱码的数据进行编码变成字节数组
        byte[] bytes = username.getBytes(StandardCharsets.UTF_8);
        //然后对自己数组进行解码
        String s = new String(bytes, StandardCharsets.UTF_8);
        //解决乱码后 username 张三*/

//        String utf_8 = URLEncoder.encode(username, "UTF_8");
//        URLDecoder.decode(utf_8,"ISO-8859-1");


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通用的获取数据
        //1.获取所有请求参数Map集合
      /*  Map<String, String[]> map = request.getParameterMap();
        for (String key : map.keySet()){
            //username  password
            System.out.println("key = " + key);

            //值是数组  循环取值
            String[] strings = map.get(key);
            for (String value : strings){
                System.out.print("值为："+value+" ");
            }

            System.out.println();
        }*/

        //2.获取请求参数  中字符串集合
       /* String[] hobbies = request.getParameterValues("username");
        for (String s : hobbies){
            System.out.println("s = " + s);
        }*/

        //post请求中文乱码 设置请求编码
        request.setCharacterEncoding("UTF-8");

        //3.根据名字获取单个请求参数
        String username = request.getParameter("username");
        System.out.println("username = " + username);


        //想要去找到另外一个资源（Servlet?）
    }
}
